package com.mg.plugin.drought.util;

import com.mg.plugin.drought.entity.StationData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AssistStationData {
    public static Map<String,List<StationData>> mapByStationId(Map<String,String> headerIndex, List<String> stationDataList) {
        Map<String,List<StationData>> resultStationData = new HashMap<String, List<StationData>>();
        if (null != stationDataList && stationDataList.size() > 2) {
            String realHeader = stationDataList.get(0);
            executeData(stationDataList, resultStationData, realHeader, headerIndex);
        }
        return resultStationData;
    }

    public static List<StationData> assistStationData(Map<String,String> headerIndex, List<String> stationDataList) {
        Map<String,List<StationData>> resultStationData = new HashMap<String, List<StationData>>();
        if (null != stationDataList && stationDataList.size() > 2) {
            String realHeader = stationDataList.get(0);
            executeData(stationDataList, resultStationData, realHeader, headerIndex);
        }
        List<StationData> data = new ArrayList<StationData>();
        for (String key:resultStationData.keySet()) {
            data.addAll(resultStationData.get(key));
        }
        return data;
    }

    private static void executeData(List<String> stationDataList, Map<String,List<StationData>> resultStationData, String realHeader, Map<String,String> headerIndex) {
        int stationIdIndex = findDataIndex(realHeader, headerIndex.get("StationId"));
        int dateIndex = findDataIndex(realHeader, headerIndex.get("Date"));
        int latIndex = findDataIndex(realHeader, headerIndex.get("Lat"));
        int altiIndex = findDataIndex(realHeader, headerIndex.get("Alti"));
        int avgTempIndex = findDataIndex(realHeader, headerIndex.get("AvgTemp"));
        int maxTempIndex = findDataIndex(realHeader, headerIndex.get("MaxTemp"));
        int minTempIndex = findDataIndex(realHeader, headerIndex.get("MinTemp"));
        int windSpeedIndex = findDataIndex(realHeader, headerIndex.get("WindSpeed"));
        int rainIndex = findDataIndex(realHeader, headerIndex.get("Rain"));
        int humidityIndex = findDataIndex(realHeader, headerIndex.get("Humidity"));
        int sunshineIndex = findDataIndex(realHeader, headerIndex.get("Sunshine"));

        for (int i = 2; i < stationDataList.size(); i++) {
            String[] data = stationDataList.get(i).split(",");
            String stationId = data[stationIdIndex];
            StationData stationData = new StationData();
            stationData.setStationId(stationId);
            stationData.setDate(data[dateIndex]);
            stationData.setLat(Float.parseFloat(data[latIndex]));
            stationData.setAlti(Float.parseFloat(data[altiIndex]));
            stationData.settAvg(Float.parseFloat(data[avgTempIndex]));
            stationData.settMax(Float.parseFloat(data[maxTempIndex]));
            stationData.settMin(Float.parseFloat(data[minTempIndex]));
            stationData.setWindSpeed(Float.parseFloat(data[windSpeedIndex]));
            stationData.setRain(Float.parseFloat(data[rainIndex]));
            stationData.setHumidity(Float.parseFloat(data[humidityIndex]));
            stationData.setSunshine(Float.parseFloat(data[sunshineIndex]));

            if (resultStationData.keySet().contains(stationId)) {
                resultStationData.get(stationId).add(stationData);
            } else {
                List<StationData> dataList = new ArrayList<StationData>();
                dataList.add(stationData);
                resultStationData.put(stationId, dataList);
            }
        }
    }

    private static int findDataIndex(String realHeader, String strRain) {
        String[] data = realHeader.split(",");
        for (int i = 0; i < data.length; i++) {
            if (data[i].equalsIgnoreCase(strRain)) {
                return i;
            }
        }
        return -1;
    }

    public static Map<String,List<StationData>> mapByStationDataId(Map<String,String> headerIndex, List<String> stationDataList,List<String> stationDataList1,List<String> stationDataList2,List<Float>per) {
        Map<String,List<StationData>> resultStationData = new HashMap<String, List<StationData>>();
        if (null != stationDataList && stationDataList.size() > 2) {
            String realHeader = stationDataList.get(0);
            executeDataForCom(stationDataList, resultStationData, realHeader, headerIndex,per.get(0));
        }
        if (null != stationDataList1 && stationDataList1.size() > 2) {
            String realHeader = stationDataList1.get(0);
            executeDataForCom(stationDataList1, resultStationData, realHeader, headerIndex,per.get(1));
        }
        if (null != stationDataList2 && stationDataList2.size() > 2) {
            String realHeader = stationDataList2.get(0);
            executeDataForCom(stationDataList2, resultStationData, realHeader, headerIndex,per.get(2));
        }
        return resultStationData;
    }

    private static void executeDataForCom(List<String> stationDataList, Map<String,List<StationData>> resultStationData, String realHeader, Map<String,String> headerIndex,Float per) {
        int stationIdIndex = findDataIndex(realHeader, headerIndex.get("StationId"));
        int dateIndex = findDataIndex(realHeader, headerIndex.get("Date"));
        int dataIndex = findDataIndex(realHeader, headerIndex.get("Data"));

        for (int i = 2; i < stationDataList.size(); i++) {
            String[] data = stationDataList.get(i).split(",");
            String stationId = data[stationIdIndex];
            StationData stationData = new StationData();
            stationData.setStationId(stationId);
            stationData.setDate(data[dateIndex]);
            stationData.setData(Float.parseFloat(data[dataIndex]));
            stationData.setPer(per);

            if (resultStationData.keySet().contains(stationId)) {
                resultStationData.get(stationId).add(stationData);
            } else {
                List<StationData> dataList = new ArrayList<StationData>();
                dataList.add(stationData);
                resultStationData.put(stationId, dataList);
            }
        }
    }
}
